<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\oms;

use think\Model;

class CompanyPoint extends Model {

    protected $table = 'company_point';
    protected $connection = ['database' => 'oms'];

    
    /**
     * 通过条件店员积分
     * @param type $condition
     * @param type $field
     * @return type
     */
    public function getCompanyPointByCondition($condition,$field='*'){
        return $this->where($condition)->field($field)->find();
    }
    
    /**
     * 添加店员积分
     * @param type $company_id
     * @param type $staff_id
     * @param type $points
     */
    public function addCompanyPoint($company_id,$staff_id,$points){
        
        $result = $this->where(['o_id'=>$company_id,'staff_id'=>$staff_id])->find();
        if($result){
            //更新
            $data = [
                'points_use'=>$result['points_use']+$points,
                'points_all'=>$result['points_all']+$points,
                'last_ts'=>time(),
            ];
            $res = $this->where([
                'id'=>$result['id'],
                'points_use'=>$result['points_use'],
                'points_all'=>$result['points_all'],
            ])->update($data);
            
            return $res;
        }
        
        $res = $this->insert([
            'o_id'=>$company_id,
            'staff_id'=>$staff_id,
            'points_use'=>$points,
            'points_all'=>$points,
            'points_frozen'=>0,
            'add_ts'=>time(),
            'last_ts'=>time()
        ]);
        
        return $res;
    }
}
